package com.xquant.trade.account.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xquant.common.core.web.controller.BaseController;
import com.xquant.common.security.annotation.RequiresPermissions;
import com.xquant.common.security.utils.SecurityUtils;
import com.xquant.trade.account.entity.Account;
import com.xquant.trade.account.service.AccountService;
import com.xquant.trade.utils.CommResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static com.xquant.common.core.utils.PageUtils.startPage;

/**
 *
 * newCloud 本方账户管理
 * */
@RestController
@RequestMapping("account")
public class AccountController extends BaseController {
    @Autowired
    private AccountService accountService;


    @RequiresPermissions("account:account:list")
    @RequestMapping(value = "/querySelfAccountByPage.action")
    public PageInfo<Account> querySelfAccountByPage(@RequestParam Map<String, Object> param) {
        startPage();
        param.put("userId", SecurityUtils.getUserId());
        List<Account> list = accountService.querySelfAccountByPage(param);
        return new PageInfo<Account>(list);
    }

    @RequiresPermissions("account:account:list")
    @RequestMapping(value = "/querySelfAccountCombox.action")
    public PageInfo<Account> querySelfAccountCombox(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        PageHelper.startPage(1, 60).setReasonable(true);
        return  new PageInfo<Account>(accountService.querySelfAccountByPage(param));
    }

    @RequestMapping(value = "/queryPartyAccountByPage.action")
    public PageInfo<Account> queryPartyAccountByPage(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        return accountService.queryPartyAccountByPage(param);
    }
    @RequiresPermissions("account:account:add")
    @RequestMapping(value = "/addAccount.action")
    public Object addAccount(Account account) {
        account.setCreater(SecurityUtils.getUserId().toString());
        boolean check = accountService.checkAccName(account);
        boolean check2 = accountService.checkAccCode(account);
        if (check&&check2){
            accountService.addAccount(account);
            return CommResponse.success("保存成功");
        }else if(!check) {
            return CommResponse.failure("账户名称重复");
        }else {
            return CommResponse.failure("账户编号重复");
        }

    }
    @RequiresPermissions("account:account:edt")
    @RequestMapping(value = "/updateAccount.action")
    public CommResponse updateAccount(Account account) {
        boolean check = accountService.checkAccName(account);
        if (check){
            accountService.updateAccount(account);
            return CommResponse.success("修改成功");
        }else {
            return CommResponse.failure("账户名称重复");
        }
    }
    @RequiresPermissions("account:account:del")
    @RequestMapping(value = "/deleteAccount.action")
    public Object deleteAccount(Account account) {
        accountService.deleteAccount(account);
        return CommResponse.success("删除成功");
    }

    @RequestMapping(value = "/saveAccountUser.action")
    public Object saveAccountUser(@RequestParam Map<String, Object> param) {
        accountService.saveAccountUser(param);
        return CommResponse.success("保存成功");
    }

    @RequestMapping(value = "/updateAccountUser.action")
    public Object updateAccountUser(@RequestBody Map<String, Object> params) {

        accountService.updateAccountUser(params);

        return CommResponse.success("修改成功");
    }

    @RequestMapping(value = "/deleteAccountUser.action")
    public Object deleteAccountUser(@RequestParam Map<String, Object> param) {
        param.put("userId", SecurityUtils.getUserId());
        accountService.deleteAccountUser(param);
        return CommResponse.success("删除成功");
    }




    @RequestMapping(value = "/queryAccountUserAndDeptPerm.action")
    public List queryAccountUserAndDeptPerm(@RequestParam Map<String,Object> param){
        List list = accountService.getAccountUserAndDeptPerm(param);
        return list;
    }

    @RequiresPermissions("account:account::allocate")
    @RequestMapping(value = "/updateAccountPerm.action")
    public CommResponse updateAccountPerm(Account account) {
        accountService.updateAccountPerm(account);
        return new CommResponse(true,"操作成功");

    }
}
